package com.activity.gaswatchermtl;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.activity.gaswatchermtl.dialog.DialogFilters;
import com.app.Const;
import com.inf8405.db.DAOStations;
import com.inf8405.db.SQLiteHelper;
import com.module.StationFinder.GasStationFinder;
import com.module.StationFinder.Station;
import com.module.StationFinder.StationDetails;
import com.module.StationFinder.AsyncTask.LoadStationDetailTask;

import android.app.Activity;
import android.app.DialogFragment;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class StationDetailsActivity extends Activity {

	// Google Places
	GasStationFinder googlePlaces;

	// Place Details
	StationDetails stationDetails;
	
	LoadStationDetailTask loadDetails_ = null;

	// Progress dialog
	ProgressDialog pDialog;
	
	DAOStations daostations_;

	// KEY Strings
	public static final String KEY_REFERENCE = "reference"; // id of the place
	public static String KEY_NAME = "name"; // name of the place
	public static String KEY_PRICE = "price";
	

	private int mIdPost = -1;
	String column_id = "id";
	String column_name = "name";
	String column_reference = "reference";
	String column_vicinity = "vicinity";
	String column_lat = "lat";
	String column_lng = "lng";
	String column_address = "formatted_address";
	String column_phone_number = "formatted_phone_number";
	ListView listView;
	
	int resIcon = R.drawable.ic_action_favorite_off;
	boolean onCreatedone =false;
	MenuItem menu_star_ = null;
	public boolean  mIsStarred= false;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_station_details);

		daostations_ = new  DAOStations (this);
		daostations_.open();
		Intent i = getIntent();

		// Place reference id
		String reference = i.getStringExtra(KEY_REFERENCE);

		// Calling a Async Background thread
		loadDetails_ = new LoadStationDetailTask(this, reference);
		loadDetails_.setTextViews((TextView) findViewById(R.id.name),
				(TextView) findViewById(R.id.address),
				(TextView) findViewById(R.id.phone), 
				(TextView) findViewById(R.id.location),
				(TextView) findViewById(R.id.price_details),
				(TextView) findViewById(R.id.price_date_details));
		loadDetails_.execute();
		onCreatedone = true;
		}

	public void getDetailsActualStation() {
	column_id = loadDetails_.getStationDetails().result.id;
	column_name = loadDetails_.getStationDetails().result.name;
	column_reference = loadDetails_.getStationDetails().result.reference;
	column_vicinity = loadDetails_.getStationDetails().result.vicinity;
	column_lat = Double.toString(loadDetails_.getStationDetails().result.geometry.location.lat);
	column_lng = Double.toString(loadDetails_.getStationDetails().result.geometry.location.lng);
	column_address = loadDetails_.getStationDetails().result.formatted_address;
	column_phone_number = loadDetails_.getStationDetails().result.formatted_phone_number;
	
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		
		if (onCreatedone)
		{
		getMenuInflater().inflate(R.menu.station_details, menu);
		resIcon = (mIsStarred ? R.drawable.ic_action_favorite_on
                : R.drawable.ic_action_favorite_off);
		menu_star_ =  menu.findItem(R.id.menu_favorites_toggle);
		menu_star_.setIcon(resIcon);
		}
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{
		//
		//mIsStarred = daostations_.ExistStation(column_id);
		switch (item.getItemId()) 
		{
		case R.id.menu_favorites_toggle:
			 onCheckedChanged(mIsStarred);
			 mIsStarred = (mIsStarred ? false : true); // Toggle value
	         this.invalidateOptionsMenu();
	         return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	private void onCheckedChanged(boolean wasFavorite) {
		int message = R.string.toast_favorites_added;
		getDetailsActualStation() ;
        if (wasFavorite) {
            /**
             * Remove from favorites.
             */
            String[] args = new String[] {
                    Integer.toString(mIdPost)
            };
            DataBaseDeleteStation(column_id);
            message = R.string.toast_favorites_removed;
        }
        else {
            /**
             * Add to favorites
             */
        	DataBaseInsertStation();
        	message = R.string.toast_favorites_added;
        }
        //daostations_.close();
    }      
	
	protected void DataBaseInsertStation(){
		DecimalFormat sf = new DecimalFormat("000.00");
		String price = sf.format((Math.random()*5+130));
		
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy 'at' HH:mm");
		String currentDateandTime = sdf.format(new Date());
		
		daostations_.InsertInfoStationDB(column_id, column_name, column_reference, "",column_vicinity, column_lat, column_lng, 
				column_address, column_phone_number, price, currentDateandTime);
		}
	protected void DataBaseDeleteStation(String id){
		daostations_.deleteStation(id);
		}

	public void onCreateStationDetails() {
		// TODO Auto-generated method stub

		DAOStations daostations_ = new  DAOStations (this);
		daostations_.open();
		String id = loadDetails_.getStationDetails().result.id;
		Log.w("MED","id="+id);
		mIsStarred = daostations_.ExistStation(id);

		resIcon = (mIsStarred ? R.drawable.ic_action_favorite_on
                : R.drawable.ic_action_favorite_off);
		menu_star_.setIcon(resIcon);
		daostations_.close();
	}
	
}

